﻿using Microsoft.Xna.Framework;
using System;

namespace XGame
{
    public class Timer
    {
        #region Fields

        public bool IsActive { get { return CurrentDuration < TotalDuration; } }
        public float PercentComplete { get { return (float)(CurrentDuration / TotalDuration); } }

        public double CurrentDuration;
        public double TotalDuration;

        public event EventHandler onTimerComplete;


        private bool isRunning;
        #endregion

        #region Initialization

        /// <summary>
        /// Start the timer
        /// </summary>
        /// <param name="seconds"></param>
        public void Start(double seconds)
        {
            CurrentDuration = 0.0;
            TotalDuration = seconds;
            isRunning = true;
        }

        private void TimerComplete(EventArgs e)
        {
            if (onTimerComplete != null)
                onTimerComplete(this, e);
        }

        #endregion

        #region Updating

        /// <summary>
        /// Update the timer
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            CurrentDuration += gameTime.ElapsedGameTime.TotalSeconds;

            if (CurrentDuration > TotalDuration&&isRunning)
            {
                CurrentDuration = TotalDuration;
                TimerComplete(EventArgs.Empty);
                isRunning = false;
            }

        }

        #endregion
    }
}
